//
//  BCDebugUserConfig.m
//  Pod
//
//  Created by Basic on 2017/3/14.
//  Copyright © 2017年 naruto. All rights reserved.
//

#import "BCDebugUserConfig.h"
#import "BCDebugUtils.h"
#import <objc/message.h>

@interface BCDebugUserConfig()

@end

@implementation BCDebugUserConfig
#pragma mark - system
- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

#pragma mark -  BCDebugKitProtocol
-(BCDebugItemGroup *)bcDebugConfigSetupDebugGroup
{
    if ([self.configDelegate respondsToSelector:@selector(bcDebugConfigSetupDebugGroup)]) {
        return [self.configDelegate bcDebugConfigSetupDebugGroup];
    }
    
    BCDebugItemGroup *debugGroup = [BCDebugItemGroup new];
    debugGroup.bcName = @"用户信息";
    debugGroup.bcConfigClass = [self class];
    //系统版本
    NSString *userId = @"";
    NSString *name = @"";
    NSString *phone = @"";
    NSString *accesstoken = @"";
    Class userComCls = NSClassFromString(@"ZHUserCom");
    if ([userComCls respondsToSelector:NSSelectorFromString(@"user")]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        id userModel = ((id (*)(id, SEL))(void *) objc_msgSend)((id)userComCls, NSSelectorFromString(@"user"));
        if ([userModel respondsToSelector:NSSelectorFromString(@"userId")]) {
            userId = (NSString *)[userModel performSelector:NSSelectorFromString(@"userId")];
        }
        if ([userModel respondsToSelector:NSSelectorFromString(@"name")]) {
            name = (NSString *)[userModel performSelector:NSSelectorFromString(@"name")];
        }
        if (name.length<=0 && [userModel respondsToSelector:NSSelectorFromString(@"username")]) {
            name = (NSString *)[userModel performSelector:NSSelectorFromString(@"username")];
        }
        if (name.length<=0 && [userModel respondsToSelector:NSSelectorFromString(@"nickname")]) {
            name = (NSString *)[userModel performSelector:NSSelectorFromString(@"nickname")];
        }
        if ([userModel respondsToSelector:NSSelectorFromString(@"phone")]) {
            phone = (NSString *)[userModel performSelector:NSSelectorFromString(@"phone")];
        }
        if ([userModel respondsToSelector:NSSelectorFromString(@"accesstoken")]) {
            accesstoken = (NSString *)[userModel performSelector:NSSelectorFromString(@"accesstoken")];
        }
#pragma clang diagnostic pop
    }
    NSMutableString *userInfo = [[NSMutableString alloc] init];
    if ([userId isEqualToString:@""]) {
        [userInfo appendString:@"未登录"];
    }
    else {
        [userInfo appendFormat:@"%@", userId];
        if (![name isEqualToString:@""]) {
            if (![userInfo isEqualToString:@""]) {
                [userInfo appendString:@","];
            }
            [userInfo appendString:name];
        }
        if (![phone isEqualToString:@""]) {
            if (![userInfo isEqualToString:@""]) {
                [userInfo appendString:@","];
            }
            [userInfo appendString:phone];
        }
        if (![accesstoken isEqualToString:@""]) {
            if (![userInfo isEqualToString:@""]) {
                [userInfo appendString:@","];
            }
            [userInfo appendString:accesstoken];
        }
    }
    BCDebugItemModel *debugItem = [BCDebugItemModel new];
    debugItem.bcName = @"基本信息";
    debugItem.bcDetail = userInfo;
    debugItem.bcSelectStyle = UITableViewCellSelectionStyleNone;
    if (userId.length>0) {
        //已登录
        debugItem.selectItemAction = ^(BCDebugItemModel * _Nonnull item, NSInteger selectRow){
            //复制内容
            [UIPasteboard generalPasteboard].string = item.bcDetail;
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"内容已复制到剪贴板" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *conform = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:nil];
            [alert addAction:conform];
            [[BCDebugUtils topPage] presentViewController:alert animated:YES completion:nil];
        };
    }
    [debugGroup.bcItems addObject:debugItem];
    
    return debugGroup;
}
@end
